package com.unionfetch.content_home.ui

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.unionfetch.content_home.R
import com.unionfetch.content_home.bean.UserStatus
import com.unionfetch.content_home.bean.findUserStatus
import com.unionfetch.content_home.ui.step.FirstStepOpenAccountActivity
import com.unionfetch.content_home.ui.step.SecondStepOpenAccountActivity
import com.unionfetch.content_home.ui.step.ThirdStepOpenAccountActivity
import com.unionftech.common.R.*
import com.unionftech.common.base.BaseActivity
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.router.RouterPath
import com.unionftech.common.utils.ToastUtils
import com.unionftech.common.utils.serializable

@Route(
    path = RouterPath.GuideCreateAccountActivityInfo.PATH,
    name = RouterPath.GuideCreateAccountActivityInfo.NAME
)
class GuideCreateAccountActivity : BaseActivity() {
    private val mBtnOpenAccountNow by lazy { findViewById<Button>(R.id.btn_open_account_now) }
    private val mTvAccountSettingsState by lazy { findViewById<TextView>(R.id.tv_account_settings_state) }
    private val mTvAccountSettings by lazy { findViewById<TextView>(R.id.tv_account_settings) }
    private val mIvNumber3 by lazy { findViewById<ImageView>(R.id.iv_number_3) }
    private val mTvAssetAppraisalState by lazy { findViewById<TextView>(R.id.tv_asset_appraisal_state) }
    private val mTvAssetAppraisal by lazy { findViewById<TextView>(R.id.tv_asset_appraisal) }
    private val mIvNumber2 by lazy { findViewById<ImageView>(R.id.iv_number_2) }
    private val mTvBaseInfoState by lazy { findViewById<TextView>(R.id.tv_base_info_state) }
    private val mTvBaseInfo by lazy { findViewById<TextView>(R.id.tv_base_info) }
    private val mLlBaseInfoContainer by lazy { findViewById<LinearLayout>(R.id.ll_base_info_container) }
    private val mTvWithoutAccountStepTip by lazy { findViewById<TextView>(R.id.tv_without_account_step_tip) }
    private val mTvCreateAccountTip by lazy { findViewById<TextView>(R.id.tv_create_account_tip) }


    private var mUserStatus: Int = 0
    private var mIsIB: Boolean = false


    companion object {

        private const val PARAMS_STATUS_KEY = "params_status_key"
        private const val PARAMS_IS_IB_KEY = "params_is_ib_key"

        @JvmStatic
        fun startThis(context: Context, status: Int, isIB: Boolean) {
            Intent(context, GuideCreateAccountActivity::class.java).apply {
                if (context !is Activity) {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                putExtra(PARAMS_STATUS_KEY, status)
                putExtra(PARAMS_IS_IB_KEY, isIB)
                context.startActivity(this)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initLayout() = R.layout.activity_guide_create_account

    @SuppressLint("NewApi")
    override fun obtainIntentData(intent: Intent) {
        super.obtainIntentData(intent)
        mUserStatus = intent.getIntExtra(PARAMS_STATUS_KEY, 0)
        mIsIB = intent.getBooleanExtra(PARAMS_IS_IB_KEY, false)
    }

    override fun setListener() {
        super.setListener()
        mBtnOpenAccountNow.setOnClickListener(this)
    }

    override fun initLanguageText() {
        super.initLanguageText()
        setBackText(DynamicResourceManager.getString(mContext, string.account_manager_title))
        mTvCreateAccountTip.text =
            DynamicResourceManager.getString(mContext, string.without_live_account)
        mTvWithoutAccountStepTip.text =
            DynamicResourceManager.getString(mContext, string.without_live_account_step_tip)
        mTvBaseInfo.text = DynamicResourceManager.getString(mContext, string.base_info)
        mTvBaseInfoState.text = DynamicResourceManager.getString(mContext, string.incomplete)
        mTvBaseInfoState.isSelected = false

        mTvAssetAppraisal.text =
            DynamicResourceManager.getString(mContext, string.asset_appraisal)
        mTvAssetAppraisalState.text =
            DynamicResourceManager.getString(mContext, string.incomplete)
        mTvAssetAppraisalState.isSelected = false

        mTvAccountSettings.text =
            DynamicResourceManager.getString(mContext, string.account_settings)
        mTvAccountSettingsState.text =
            DynamicResourceManager.getString(mContext, string.incomplete)
        mTvAccountSettingsState.isSelected = false

        mBtnOpenAccountNow.text =
            DynamicResourceManager.getString(mContext, string.open_account_now)
    }

    private fun initTitleBar() {
        setBackText(
            DynamicResourceManager.getString(mContext, string.account_manager_title),
            mContext.getColor(
                color.color_1C1D21
            )
        )
        hideCenterTitle()
        showBackImg(drawable.title_back)
        setRightImg(drawable.ic_headphones) {
            ToastUtils.longToast(
                DynamicResourceManager.getString(
                    mContext, string.developing
                )
            )
        }
    }

    override fun setView() {
        super.setView()
        if (mIsIB) {
            mIvNumber2.setImageResource(R.drawable.ic_number_1)
            mIvNumber3.setImageResource(R.drawable.ic_number_2)
        } else {
            mIvNumber2.setImageResource(R.drawable.ic_number_2)
            mIvNumber3.setImageResource(R.drawable.ic_number_3)
        }
        initTitleBar()
        setupStatusInfo()
    }

    private fun setupStatusInfo() {
        if (mIsIB) {
            mLlBaseInfoContainer.visibility = View.GONE
            mTvWithoutAccountStepTip.text = DynamicResourceManager.getString(
                mContext, string.without_live_account_step_ib_tip
            )
        } else {
            mLlBaseInfoContainer.visibility = View.VISIBLE
            mTvWithoutAccountStepTip.text =
                DynamicResourceManager.getString(mContext, string.without_live_account_step_tip)
        }
        when (mUserStatus) {
            UserStatus.TRADER_ASSESSMENT.status -> {
                mTvBaseInfoState.isSelected = true
                mTvBaseInfoState.text = DynamicResourceManager.getString(string.completed)
                mTvAssetAppraisalState.isSelected = true
                mTvAssetAppraisalState.text = DynamicResourceManager.getString(string.completed)
                mTvAccountSettings.isSelected = true
                mTvAccountSettings.text = DynamicResourceManager.getString(string.completed)
            }

            UserStatus.TRADER_ACCOUNT_SETTING.status -> {
                mTvBaseInfoState.isSelected = true
                mTvBaseInfoState.text = DynamicResourceManager.getString(string.completed)
                mTvAssetAppraisalState.isSelected = true
                mTvAssetAppraisalState.text = DynamicResourceManager.getString(string.completed)
                mTvAccountSettings.isSelected = false
            }

            UserStatus.COUNTRY_SETTING.status -> {
                mTvBaseInfoState.isSelected = true
                mTvBaseInfoState.text = DynamicResourceManager.getString(string.completed)
                mTvAssetAppraisalState.isSelected = false
                mTvAccountSettings.isSelected = false
            }

            UserStatus.TRADER_OPEN_ACCOUNT.status -> {
                mTvBaseInfoState.isSelected = false
                mTvAssetAppraisalState.isSelected = false
                mTvAccountSettings.isSelected = false
            }
        }
    }


    override fun doClick(v: View?) {
        super.doClick(v)
        when (v?.id) {
            R.id.btn_open_account_now -> {
                //跳转立即开户
                gotoOpenAccountStep()
            }
        }
    }

    private fun gotoOpenAccountStep() {
        if (mIsIB) {
            SecondStepOpenAccountActivity.startThis(mContext, true)
            finish()
            return
        }
        when (mUserStatus) {
            UserStatus.TRADER_ACCOUNT_SETTING.status -> {
                ThirdStepOpenAccountActivity.startThis(mContext)
            }

            UserStatus.COUNTRY_SETTING.status -> {
                SecondStepOpenAccountActivity.startThis(mContext, false)
            }

            UserStatus.TRADER_OPEN_ACCOUNT.status -> {
                FirstStepOpenAccountActivity.startThis(mContext)
            }
        }
        finish()
    }
}